package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.entity.UserVO;
import com.example.demo.entity.UserDTO;
import com.example.demo.properties.JwtProperties;
import com.example.demo.result.Result;
import com.example.demo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 用户管理
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 用户登录
     * @param userDTO
     * @return
     */
    @PostMapping("/login")
    public Result<UserVO> login(@RequestBody UserDTO userDTO) {
        log.info("用户登录：{}", userDTO);

        User user = userService.login(userDTO);

        //登录成功后，生成jwt令牌
//            Map<String, Object> claims = new HashMap<>();
//            claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
//            String token = JwtUtil.createJWT(
//                    jwtProperties.getAdminSecretKey(),
//                    jwtProperties.getAdminTtl(),
//                    claims);

        UserVO userVO = UserVO.builder()
                .user_id(user.getUser_id())
                .username(user.getUsername())
                .credit(user.getCredit())
                .today_get(user.getToday_get())
//                    .token(token)
                .build();

        return Result.success(userVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    @PostMapping("/register")
    public Result<UserVO> register(@RequestBody UserDTO userDTO) {
        log.info("新增用户：{}", userDTO);
        userService.save(userDTO);
        return Result.success();
    }
}

